<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                <el-row>为角色分配菜单和资源</el-row>
                <el-row v-model="role.name" style="height: 30px;line-height: 30px;">
                    当前分配角色为：{{ (role.name && role.name.length > 0) ? role.name : "无"}}
                </el-row>
            </el-header>
            <el-main>
                <el-collapse v-model="activeNames" accordion>
                    <el-collapse-item title="分配资源" name="1">
                        <el-row type="flex" justify="space-around" style="padding: 15px 0;">
                            <el-transfer
                                    v-model="selResUuids"
                                    :data="resData"
                                    :titles="['未授权资源','已授权资源']"
                                    filterable
                                    @change="resourcesChange"
                                    :props="resProps"
                            ></el-transfer>
                        </el-row>
                    </el-collapse-item>
                    <el-collapse-item title="分配菜单" name="2">
                        <el-row type="flex" justify="space-start" style="padding: 15px 0;">
                            <el-tree
                                    ref="menuTree"
                                    :data="menuData"
                                    node-key="uuid"
                                    default-expand-all
                                    show-checkbox
                                    :check-strictly="true"
                                    :expand-on-click-node="false"
                                    :props="menuProps"
                                    @check-change="checkChange"
                                    max-height="288"
                            ></el-tree>
                        </el-row>
                    </el-collapse-item>
                </el-collapse>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>
                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="editOut">
                        返回
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {getDataForSP, writePageInfo} from "../../../utils/webUtils.js";

    export default {
        name: "allocationsRole",
        data() {
            return {
                formAlignLeft: 'left',
                activeNames: '1',
                uuid: this.roleUuid,
                role: {},
                //菜单数据
                menuData: [],
                //菜单props
                menuProps: {
                    children: 'subMenus',
                    label: 'name'
                },
                resData: [],
                selResUuids: [],
                resProps: {
                    key: "uuid",
                    label: "name"
                },
                resourcePage: {
                    pageSize: 99999,//每页显示条目个数
                    currentPage: 1,//当前页数
                    totalElements: 0,
                    searchData: {}
                },
                info: '',
                infoShow: false
            }
        },
        props: {
            roleUuid: {
                type: String
            }
        },
        //深度监听父组件的props传值
        watch: {
            roleUuid(val) {
                this.uuid = val;
                this.queryRole();
                this.queryMenu();
                this.queryRolesMenu();
                this.queryResources();
                this.queryRoleResources();
            },
            other: {
                //深度观察对象
                handler(val) {
                },
                deep: true
            }
        },
        mounted() {
            this.queryRole();
            this.queryMenu();
            this.queryRolesMenu();
            this.queryResources();
            this.queryRoleResources();
        },
        methods: {
            queryRole() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/role/" + this.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.role = response.data;
                }).catch(error => {
                    console.log(error);
                });
            },
            //查询全量菜单
            queryMenu() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/menu/search/findAllTree",
                    params: {}
                }).then(response => {
                    this.menuData = response.data;
                }).catch(error => {
                    console.log(error);
                });
            },
            //查询角色的菜单对象
            queryRolesMenu() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/role/" + this.uuid + "/menu"
                }).then(response => {
                    let rolMn = [];
                    for (let dt of response.data) {
                        rolMn.push(dt.uuid);
                    }
                    this.$refs["menuTree"].$off("check-change");
                    this.$refs["menuTree"].setCheckedKeys(rolMn);
                    this.$nextTick(function () {
                        this.$refs["menuTree"].$on("check-change", this.checkChange);
                    });
                }).catch(error => {
                    console.log(error);
                });
            },
            //菜单选中操作
            checkChange(data, checked) {
                if (checked) {
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/role/" + this.uuid + "/menu",
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        data: this.qs.stringify({
                            menuUuid: data.uuid
                        })
                    }).then(response => {
                        this.info = '分配菜单成功';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.info = '';
                        }, 1600);
                    }).catch(error => {
                        console.log(error);
                    });
                } else {
                    this.axios.request({
                        method: "delete",
                        url: "./kwp/kdi/role/" + this.uuid + "/menu/" + data.uuid,
                        headers: {},
                        data: {}
                    }).then(response => {
                        this.info = '移除菜单成功';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.info = '';
                        }, 1600);
                    }).catch(error => {
                        console.log(error);
                    });
                }
            },
            //获取全量资源
            queryResources() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/res/search/findAll",
                    params: getDataForSP(this.resourcePage)
                }).then(response => {
                    //全量资源
                    this.resData = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            //获取角色的资源
            queryRoleResources() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/role/" + this.uuid + "/res",
                    params: getDataForSP(this.resourcePage)
                }).then(response => {
                    this.selResUuids = [];
                    for (let res of response.data) {
                        this.selResUuids.push(res.uuid);
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            //穿梭框元素变化时触发
            resourcesChange(currentValues, direction, movedValues) {
                //currentValues 当前框的数值 ； direction 转移的方向 ；movedValues  移动的数值
                //direction = right 为 添加 ；direction = left 为 移除
                if (direction === "right") {
                    let resUuidArray = [];
                    for (let x = 0; x <= movedValues.length; x++) {
                        resUuidArray.push(movedValues[x]);
                    }
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/role/" + this.uuid + "/res",
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        data: this.qs.stringify({
                            resUuids: resUuidArray
                        }, {arrayFormat: 'brackets'})
                    }).then(response => {
                        this.info = '分配资源成功';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.info = '';
                        }, 1600);
                    }).catch(error => {
                        console.log(error);
                    });
                } else {
                    for (let x = 0; x <= movedValues.length; x++) {
                        this.axios.request({
                            method: "delete",
                            url: "./kwp/kdi/role/" + this.uuid + "/res/" + movedValues[x],
                            headers: {},
                            data: {}
                        }).then(response => {
                            this.info = '移除资源成功';
                            this.infoShow = true;
                            setTimeout(() => {
                                this.infoShow = false;
                                this.info = '';
                            }, 1600);
                        }).catch(error => {
                            console.log(error);
                        });
                    }

                }
            },
            editOut() {
                this.infoShow = false;
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 600px;
        height: 100%;
        z-index: 100;
    }
</style>